package view;

import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;

import adamsLibrary.JComboBoxAuto;
import adamsLibrary.JComboBoxAutoLinkedList;

import control.OrderControl;
import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import java.awt.Color;

public class SearchPalletsPanel extends JPanel {
	private OrderControl oc;
	private JComboBoxAuto barcodeComboBox;
	private JTextField barcodeTextField;
	private JList list;
	private DefaultListModel listModel;
	private JComboBox productComboBox;
	private JButton btnProducts;
	private JLabel lblBarcode;
	private JLabel lblDate;
	private JLabel lblTime;
	private JLabel lblBlocked;
	private JLabel lblBarcode_1;
	private JLabel lblRecipe;
	private JLabel lblRecipe_1;
	private JLabel lblOId;
	private JLabel lblDate_1;
	private JLabel lblCustomerName;
	
	
	public SearchPalletsPanel(OrderControl oc) {
		setBackground(Color.BLACK);
		this.oc=oc;

		listModel = new DefaultListModel();
		list = new JList(listModel);
		list.setBackground(Color.BLACK);
		list.setForeground(Color.GREEN);
		
		barcodeComboBox = new JComboBoxAuto(new GetBarcodeList());
		JButton btnBarcodes = new JButton("Show info");
		btnBarcodes.addActionListener(new searchBarcodeAction());
		
		productComboBox = new JComboBoxAuto(new GetProductList());
		btnProducts = new JButton("Search");
		btnProducts.addActionListener(new searchProductAction());
		
		lblBarcode = new JLabel("Barcode");
		lblBarcode.setForeground(Color.GREEN);
		
		lblDate = new JLabel("Date");
		lblDate.setForeground(Color.GREEN);
		
		lblTime = new JLabel("Time");
		lblTime.setForeground(Color.GREEN);
		
		lblBlocked = new JLabel("Block");
		lblBlocked.setForeground(Color.GREEN);
		
		lblBarcode_1 = new JLabel("Barcode");
		lblBarcode_1.setForeground(Color.GREEN);
		
		lblRecipe = new JLabel("Product");
		lblRecipe.setForeground(Color.GREEN);
		
		lblRecipe_1 = new JLabel("Product");
		lblRecipe_1.setForeground(Color.GREEN);
		
		lblOId = new JLabel("Ord.ID");
		lblOId.setForeground(Color.GREEN);
		
		lblDate_1 = new JLabel("Date");
		lblDate_1.setForeground(Color.GREEN);
		
		lblCustomerName = new JLabel("Customer Name");
		lblCustomerName.setForeground(Color.GREEN);
		
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblBarcode_1)
						.addComponent(lblRecipe))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(list, GroupLayout.DEFAULT_SIZE, 677, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblBarcode)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblDate, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(lblTime, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblBlocked)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblRecipe_1)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblOId)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblDate_1)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lblCustomerName))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(barcodeComboBox, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(productComboBox, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(btnProducts, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
								.addComponent(btnBarcodes))))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(7)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(81)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblBarcode)
								.addComponent(lblDate)
								.addComponent(lblTime, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblBlocked)
								.addComponent(lblRecipe_1)
								.addComponent(lblOId)
								.addComponent(lblDate_1)
								.addComponent(lblCustomerName)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblBarcode_1)
								.addComponent(barcodeComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(btnBarcodes))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblRecipe)
								.addComponent(productComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(btnProducts))))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(list, GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
					.addContainerGap())
		);
		setLayout(groupLayout);
		
	}
	
	private class GetBarcodeList implements JComboBoxAutoLinkedList{	
		@Override
		public LinkedList<String> getList(String string) {
			return oc.getBarcodes(string);
		}		
	}
	private class searchBarcodeAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			String barcode = barcodeComboBox.getEditor().getItem().toString();
			String info = oc.getPalletInfo(barcode);
			listModel.clear();
			listModel.addElement(info);
		}	
	}
	
	
	
	private class GetProductList implements JComboBoxAutoLinkedList{	
		@Override
		public LinkedList<String> getList(String string) {
			return oc.getProducts(string);
		}		
	}
	private class searchProductAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			String product = productComboBox.getEditor().getItem().toString();
			
			LinkedList<String> infoList = oc.getPalletsByProduct(product);
			listModel.clear();
			for(String info:infoList){
				listModel.addElement(info);
			}
		}	
	}
}
